//
//  LZImagePickerManager.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/30.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZImagePickerManager.h"
#import <AVFoundation/AVFoundation.h>
#import<AssetsLibrary/AssetsLibrary.h>
#import "LZAlertView.h"
#import "AppDelegate.h"


@interface LZImagePickerManager ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation LZImagePickerManager

- (void)choosePictureWithSourceType:(NSInteger)sourceType andPresenterVC:(UIViewController*)vc
{
    // 先校验权限
    switch (sourceType) {
        case UIImagePickerControllerSourceTypeCamera:
        {
            // 判断是否支持相机
            if ([self checkCameraPermissions]) {
                [self presentUIImagePickerController:sourceType andPresenterVC:vc];
            }
            else {
                LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"iphone_permissions_camera_remind", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"cancel", nil) otherButtonTitles:LZLocalizedString(@"iphone_permissions_go_setting", nil),nil];
                [alert show];
                
                alert.clickButtonIndexBlock = ^(NSInteger index) {
                    if (index == 1) {
                        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                    }
                };
            }
        }
            break;
        case UIImagePickerControllerSourceTypePhotoLibrary:
        {
            // 判断是否支持相册
            if ([self checkAlbumPermissions]) {
                [self presentUIImagePickerController:sourceType andPresenterVC:vc];
            }
            else {
                LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"iphone_permissions_album_remind", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"cancel", nil) otherButtonTitles:LZLocalizedString(@"iphone_permissions_go_setting", nil),nil];
                [alert show];
                
                alert.clickButtonIndexBlock = ^(NSInteger index) {
                    if (index == 1) {
                        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                    }
                };
            }
        }
            break;
            
        default:
            break;
    }
    
    
}

/* 校验相机权限 */
- (BOOL)checkCameraPermissions {
    NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
    AVAuthorizationStatus authorStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
    if(authorStatus == AVAuthorizationStatusRestricted || authorStatus == AVAuthorizationStatusDenied){
        //无权限
        return NO;
    }
    return YES;
}

/* 校验相册权限 */
- (BOOL)checkAlbumPermissions {
    ALAuthorizationStatus authorStatus = [ALAssetsLibrary authorizationStatus];
    if(authorStatus == ALAuthorizationStatusRestricted || authorStatus ==ALAuthorizationStatusDenied){
        //无权限
        return NO;
    }
    return YES;
}

#pragma mark - 调用 UIImagePickerController
- (void)presentUIImagePickerController:(NSInteger)sourceType andPresenterVC:(UIViewController*)vc {
    //    AppDelegate *appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    //    UINavigationController *nav = (UINavigationController*)appdelegate.window.rootViewController;
    //    UIViewController *vc = nav.viewControllers.lastObject;
    
    if (vc == nil) {
        AppDelegate *appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        UITabBarController *tabbarController = (UITabBarController*)appdelegate.window.rootViewController;
        UINavigationController *nav = tabbarController.selectedViewController;
        vc = nav.viewControllers.lastObject;
    }
    
    
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    
    imagePickerController.delegate = self;
    
    imagePickerController.allowsEditing = self.allowsEditing;
    
    imagePickerController.sourceType = sourceType;
    
    [vc presentViewController:imagePickerController animated:YES completion:nil];
}

#pragma mark -UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(LZPictureManagerFinishPickingMediaWithInfo:)]) {
        [self.delegate LZPictureManagerFinishPickingMediaWithInfo:info];
    }
    
    [picker dismissViewControllerAnimated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }];
}

@end
